package com.lyj.ai.controller;

import org.springframework.ai.audio.transcription.AudioTranscriptionPrompt;
import org.springframework.ai.audio.transcription.AudioTranscriptionResponse;
import org.springframework.ai.openai.OpenAiAudioSpeechModel;
import org.springframework.ai.openai.OpenAiAudioSpeechOptions;
import org.springframework.ai.openai.OpenAiAudioTranscriptionModel;
import org.springframework.ai.openai.OpenAiAudioTranscriptionOptions;
import org.springframework.ai.openai.api.OpenAiAudioApi;
import org.springframework.ai.openai.audio.speech.SpeechPrompt;
import org.springframework.ai.openai.audio.speech.SpeechResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 文生语音、语音转文本
 */
@RestController
public class AudioController {

    @Autowired
    OpenAiAudioSpeechModel openAiAudioSpeechModel;

    @Autowired
    OpenAiAudioTranscriptionModel openAiAudioTranscriptionModel;

    /**
     * 文本转语音
     * @return
     */
    @RequestMapping("/ai/audio")
    public String img() {
        OpenAiAudioSpeechOptions speechOptions = OpenAiAudioSpeechOptions.builder()
                .model("tts-1")
                .voice(OpenAiAudioApi.SpeechRequest.Voice.ALLOY)
                .responseFormat(OpenAiAudioApi.SpeechRequest.AudioResponseFormat.MP3)
                .speed(1.0f)
                .build();

        SpeechPrompt speechPrompt = new SpeechPrompt("Hello, my name is jack", speechOptions);
        SpeechResponse response = openAiAudioSpeechModel.call(speechPrompt);
        try {
            writeToLocal(response.getResult().getOutput());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return "ok";
    }

    /**
     * 保存到本地
     * @param bytes
     * @throws IOException
     */
    private static void writeToLocal(byte[] bytes ) throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream("D:\\temp\\lyj.mp3");
        fileOutputStream.write(bytes);
        fileOutputStream.flush();
        fileOutputStream.close();
    }

    /**
     * 语音转文本
     * @return
     */
    @RequestMapping("/ai/audioToText")
    public String audioToText() {


        OpenAiAudioTranscriptionOptions transcriptionOptions = OpenAiAudioTranscriptionOptions.builder()
                .language("en")
                .prompt("Ask not this, but ask that")
                .temperature(0f)
                .responseFormat(OpenAiAudioApi.TranscriptResponseFormat.TEXT)
                .build();
        Resource audioFile = new FileSystemResource("D:\\temp\\lyj.mp3");
        AudioTranscriptionPrompt transcriptionRequest = new AudioTranscriptionPrompt(audioFile, transcriptionOptions);
        AudioTranscriptionResponse response = openAiAudioTranscriptionModel.call(transcriptionRequest);
        return response.getResult().getOutput().toString();
    }
}
